const ex = require('express')

//2. 
const router = ex.Router()

// 3. 构造封装返回值

class RespData {
    constructor(respObj){
        const { code , message , data } = {...respObj}
        this.code = code || 4000
        this.message = message || "空"
        this.data = data || {}
    }

    static success( respData ){
        if ( respData && respData.code != 2000 ){
            respData.code = 2000
        }
        return new RespData({...respData}) 
    }

    static error( respData ){
        if ( respData && respData.code != 4000 ){
            respData.code = 4000
        }
        return new RespData({...respData}) 
    }

}

// 测试
router.get('/user/list' , (req , resp )=>{
    resp.send(RespData.success({message:'查询成功！！！' , 
    data : [
        {name : 'n1' , age: 11 },
        {name : 'n2' , age: 12 },
]
}))
})
router.get('/user/error' , (req , resp )=>{
    resp.send(RespData.error({message:'查询失败！！！'}))
})


module.exports = router 